import 'dart:convert';
import 'package:fate_blind_box/config/ly_storage.dart';
import 'package:fate_blind_box/model/chat_friend_message_entity.dart';
import 'package:fate_blind_box/network/http_config.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Provider {

  Provider._();

  static String imgUrl = "https://pic3.zhimg.com/v2-58d652598269710fa67ec8d1c88d8f03_r.jpg?source=1940ef5c";
  static String imgMaleDef = HttpConfig.normalPicUrl + "user-male.png";
  static String imgFemaleDef = HttpConfig.normalPicUrl + "user-female.png";

  //协议弹框
  static String xieyi = "";

  //红娘类型（1 个人  2 机构）
  static int matchType = 0;

  static String mobile = "";
  static String memberId = "";
  static String nickName = "";
  static String profilePhoto = "";
  static String grma = "";
  //环信id
  static String userEaseName = "";
  static String token = "";
  static String userEaseToken = "";
  static Map memberInfo = {};

  static String loginSign = "";

  static String wechatCode = "";
  static String wechatState = "";
  static String wechatAppID = kIsWeb ? "wx0b650b12ac5103bc" : "wx20bdf249241ce720";
  // static String wechatAppSecret = kIsWeb ? "3de9500a64bbb3a9ce4c8a4b73af5799" : "8286ba0b6ca4559b41753e3667538334";
  static String wechatAppSecret = kIsWeb ? "3de9500a64bbb3a9ce4c8a4b73af5799" : "0a1bb195f147c00d4eefbd85dbba34ba";
  // 当前用户信息
  static final Rx<ChatFriendMessageEntity?> _userInfo = ChatFriendMessageEntity().obs;
  // 获取当前用户信息
  static Rx<ChatFriendMessageEntity?> get userInfo => _userInfo;


  static int tabId = 0;

  static Future<String> getUserEaseName() async{
    if (userEaseName.isEmpty) {
      userEaseName = await getCache("username");
    }
    return userEaseName;
  }

  static Future<dynamic> cache(String key, dynamic value) async {
    final cache = await SharedPreferences.getInstance();
    if (value is int) {
      cache.setInt(key, value);
    } else if (value is double) {
      cache.setDouble(key, value);
    } else if (value is bool) {
      cache.setBool(key, value);
    } else if (value is String) {
      cache.setString(key, value);
    } else if (value is List<String>) {
      cache.setStringList(key, value);
    }
  }

  static String fixAvatar(String? avatarPath) {
    return avatarPath == null || avatarPath.isEmpty ||!avatarPath.contains("http")? imgMaleDef : avatarPath;
  }

  static Future<dynamic> getCache(String key) async {
    final cache = await SharedPreferences.getInstance();
    return cache.get(key)??"";
  }

  static Future<dynamic>? remove(String key) async {
    final cache = await SharedPreferences.getInstance();
    return cache.remove(key);
  }

  // 存储用户信息
  static Future<bool> setUserInfo(ChatFriendMessageEntity userInfoEntity, {bool reset = true}) async {
    _userInfo.value = userInfoEntity;
    var success = true;
    /// 存到本地
    if (reset) {
      success = await LyStorage.setString(
          key: 'meet_login_model_key', value: json.encode(userInfoEntity));
    }
    return success;
  }

  static toLogin() {

    if(kIsWeb) {
      // ProfileCompleteV3Ui
      // Navigator.of(navKey.currentState!.overlay!.context).pushAndRemoveUntil(
      //     MaterialPageRoute(builder: (context) =>
      //     /* kIsWeb ? */const LoginByWechat()/* : const LoginUi()*/),
      //         (route) => route == null
      //   // /* kIsWeb ? */const LoginByWechat()/* : const LoginUi()*/),
      //   //     (route) => route == null
      // );
    } else {
      // Navigator.of(navKey.currentState!.overlay!.context).pushAndRemoveUntil(
      //     MaterialPageRoute(builder: (context) =>
      //     /* kIsWeb ? */const LoginByPwd()/* : const LoginUi()*/),
      //         (route) => route == null
      // );
    }
  }

  // static Future toPage(Widget page) {
  //   return Navigator.of(navKey.currentState!.overlay!.context).push(MaterialPageRoute(builder: (ctx) => page));
  // }

}
